# -*- coding: utf-8 -*-
from bson import ObjectId
from config import DEFAULT_USER_AVATAR_URL
from model import Collection, db
from time import time
import hashlib


class User(Collection):
    '''
* `_id` (ObjectId) - 用户 ID
* `answerList` ([string]) - 用户回答的列表(限教师)
* `askMeList` ([string]) - ask me list of user (teacher only)
* `askList` ([string]) - ask list of user (student only)
* `avatarUrl` (string) - Url of avatar
* `collection` ([string]) - collection of user
* `commentList` ([string]) - comment list of user (student only)
* `email` (string) - email of user
* `fan` ([string]) - fan of user
* `follow` ([string]) - people followed by user
* `gender` (int) - 0 means female, 1 means male, 2 means undetermined
* `grade` (int) - 0 表示初中，1表示高一，2表示高二，3表示高三，4表示大学，5表示业余
* `isTeacher` (int) - 1 表示是教师，0 表示非教师
* `name` (string) - user name
* `password` (string) - password of user
* `phone` (int) - phone of user
* `school` (string) - school of user
* `studioId` (string) - 若该用户为教师，该字段表示其所属的画室，可为空
* `subscription` ([string]) - subscription of user
* `workList` ([string]) - work list of user, update when student post their works
* `place` (dict) - place of user
    * `province` (string) - province of user
    * `city` (string) - city of user
* `register` (int) - 1 if user is verified
* `registerTime` (long) - 用户注册时间(时间戳)
* `wechat` (dict) - user info from wechat

    * `pId` (string) - ID of wechat
    * `pToken` (string) - Token of wechat
* `weibo` (dict) - user info from weibo
    * `pId` (string) - ID of weibo
    * `pToken` (string) - Token of weibo
    '''

    collection = db.user
    current_id = None

    class Field:
        _id = '_id'
        answerList = 'answerList'
        askList = 'askList'
        askMeList = 'askMeList'
        commentList = 'commentList'
        avatarUrl = 'avatarUrl'
        collection = 'collection'
        email = 'email'
        fan = 'fan'
        follow = 'follow'
        gender = 'gender'
        grade = 'grade'
        isTeacher = 'isTeacher'
        name = 'name'
        password = 'password'
        phone = 'phone'
        school = 'school'
        studioId = 'studioId'
        subscription = 'subscription'
        workList = 'workList'
        place = 'place'
        register = 'register'
        registerTime = 'registerTime'
        weibo = 'weibo'

        class Weibo(object):
            pId = 'weibo.pId'
            pToken = 'weibo.pToken'

        class Wechat(object):
            pId = 'wechat.pId'
            pToken = 'wechat.pToken'

    class Gender(object):
        female = 0
        male = 1
        other = 2

    class Grade(object):
        chuzhong = 0
        gaoyi = 1
        gaoer = 2
        gaosan = 3
        daxue = 4
        yeyu = 5

    class Place(object):
        city = 'city'
        province = 'province'

    class Register(object):
        unverified = 0
        verified = 1

    class Weibo(object):
        pId = 'pId'
        pToken = 'pToken'

    class Wechat(object):
        pId = 'pId'
        pToken = 'pToken'

    class Relation(object):
        noRelation = 0
        follow = 1
        fan = 2
        bothWay = 3

    collection.ensure_index(Field.email, sparse=True, unique=True)
    collection.ensure_index(Field.phone, sparse=True, unique=True)
    collection.ensure_index(Field.Weibo.pId, sparse=True, unique=True)
    collection.ensure_index(Field.Wechat.pId, sparse=True, unique=True)

    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        return unicode(self.current_id)

    def __repr(self):
        return 'I am User: %s' % self.current_id
    
    @staticmethod
    def get_hashed_password(password):
        md5 = hashlib.md5()
        md5.update(password)
        hash_password = md5.hexdigest()
        return hash_password

# kind = 1: check using email
# kind = 2: check using phone number
    @staticmethod
    def checkUser(key, kind):
        if kind == 1:
            if User.find({User.Field.email: key}) != None:
                return False
        else:
            if User.find({User.Field.phone: key}) != None:
                return False


    @staticmethod
    def new_user(email, name, password):
        hash_password = User.get_hashed_password(password)
        return User.insert({
            User.Field.answerList: [],
            User.Field.askList: [],
            User.Field.askMeList: [],
            User.Field.avatarUrl: DEFAULT_USER_AVATAR_URL,
            User.Field.collection: [],
            User.Field.commentList: [],
            User.Field.email: email,
            User.Field.fan: [],
            User.Field.follow: [],
            User.Field.gender: User.Gender.other,
            User.Field.grade: User.Grade.yeyu,
            User.Field.isTeacher: 0,
            User.Field.name: unicode(name),
            User.Field.password: hash_password,
            User.Field.school: '',
            User.Field.subscription: [],
            User.Field.workList: [],
            User.Field.place: {
                User.Place.city: '',
                User.Place.province: '',
            },
            User.Field.register: User.Register.verified,
            User.Field.registerTime: int(time() * 1000)
        })

    @staticmethod
    def new_user_with_phone(phone, name, password):
        hash_password = User.get_hashed_password(password)
        return User.insert({
            User.Field.answerList: [],
            User.Field.askList: [],
            User.Field.askMeList: [],
            User.Field.avatarUrl: DEFAULT_USER_AVATAR_URL,
            User.Field.collection: [],
            User.Field.commentList: [],
            User.Field.fan: [],
            User.Field.follow: [],
            User.Field.gender: User.Gender.other,
            User.Field.grade: User.Grade.yeyu,
            User.Field.isTeacher: 0,
            User.Field.name: unicode(name),
            User.Field.password: hash_password,
            User.Field.phone: phone,
            User.Field.school: '',
            User.Field.subscription: [],
            User.Field.workList: [],
            User.Field.place: {
                User.Place.city: '',
                User.Place.province: '',
            },
            User.Field.register: User.Register.verified,
            User.Field.registerTime: int(time() * 1000)
        })

    @staticmethod
    def new_user_from_weibo(uid, name, avatar_url, access_token):
        return User.insert({
            User.Field.answerList: [],
            User.Field.askList: [],
            User.Field.askMeList: [],
            User.Field.avatarUrl: avatar_url,
            User.Field.collection: [],
            User.Field.commentList: [],
            User.Field.fan: [],
            User.Field.follow: [],
            User.Field.gender: User.Gender.other,
            User.Field.grade: User.Grade.yeyu,
            User.Field.isTeacher: 0,
            User.Field.name: name,
            User.Field.password: '',
            User.Field.school: '',
            User.Field.subscription: [],
            User.Field.workList: [],
            User.Field.place: {
                User.Place.city: '',
                User.Place.province: '',
            },
            User.Field.register: User.Register.verified,
            User.Field.registerTime: int(time() * 1000),
            User.Field.weibo: {
                User.Weibo.pId: uid,
                User.Weibo.pToken: access_token,
            }
        })
